package com.crane.oss.web.controller.sports;


import com.crane.common.core.dto.MultiResponse;
import com.crane.common.framework.utils.BeanUtil;
import com.crane.oss.school.bean.request.QueryAreaReq;
import com.crane.oss.sports.XunFeiCommonApiService;
import com.crane.oss.xunfei.req.QueryDeviceInfoBaseStationReq;
import com.crane.oss.xunfei.req.XunFeiApiRequest;
import com.crane.oss.sports.bean.response.common.EquipmentDetailResp;
import com.crane.oss.sports.bean.response.common.EquipmentListResp;
import com.crane.oss.sports.bean.resquest.QueryEquipmentDetailResp;
import com.crane.oss.web.BaseBusinessController;
import com.crane.oss.xunfei.resp.ApiListResponse;
import com.crane.oss.xunfei.resp.DeviceInfoBaseStationResp;
import com.crane.oss.xunfei.resp.DeviceInfoResp;
import com.crane.oss.xunfei.resp.DeviceInfoStatisticResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Api(value = "SchoolEquipmentController", tags = {"设备管理"})
@RestController
@RequestMapping("/equipment")
public class EquipmentController extends BaseBusinessController {

    @Resource
    XunFeiCommonApiService xunFeiCommonApiService;

    @ApiOperation(value = "获取设备列表信息", notes = "获取列表信息")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryAreaReq")
    @PostMapping("/list")
    public MultiResponse<EquipmentListResp> list(@RequestBody QueryAreaReq request) {

        List<EquipmentListResp> list= null;
        XunFeiApiRequest xunFeiApiRequest=new XunFeiApiRequest();
        ApiListResponse<DeviceInfoResp> response=xunFeiCommonApiService.getDeviceInfo(xunFeiApiRequest,getPartyId().toString(),null,getUserId().toString());
        if(null != response.getMsg() & response.getMsg().equals("success")){
            list= BeanUtil.copyList(response.getData(),EquipmentListResp.class);
        }
        ApiListResponse<DeviceInfoStatisticResp> statisticRespApiListResponse=xunFeiCommonApiService.getDeviceInfoStatistic(xunFeiApiRequest,getPartyId().toString(), null,getUserId().toString());
        if(null != response.getMsg() & response.getMsg().equals("success")){
             for(EquipmentListResp equipmentListResp : list){
                  for(DeviceInfoStatisticResp deviceInfoStatisticResp : statisticRespApiListResponse.getData()){
                        if(equipmentListResp.getId().equals(deviceInfoStatisticResp.getKeyId())){
                            equipmentListResp.setCampusName(deviceInfoStatisticResp.getCampusName());
                            equipmentListResp.setFhlNum(deviceInfoStatisticResp.getCapacityCount());
                            equipmentListResp.setHanglightNum(deviceInfoStatisticResp.getHandLampCount());
                            equipmentListResp.setHwscaleNum(deviceInfoStatisticResp.getBalanceCount());
                            equipmentListResp.setRingNum(deviceInfoStatisticResp.getRingCount());
                            equipmentListResp.setRopeNum(deviceInfoStatisticResp.getRopeCount());
                            equipmentListResp.setZwtqqNum(deviceInfoStatisticResp.getFlexionCount());
                        }
                  }
             }
             return MultiResponse.ofWithoutTotal(list);
        }
        return MultiResponse.ofWithoutTotal(new ArrayList<>());
    }


    @ApiOperation(value = "获取设备详情", notes = "获取设备详情")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryEquipmentDetailResp")
    @PostMapping("/detail")
    public MultiResponse<EquipmentDetailResp> list(@RequestBody QueryEquipmentDetailResp request) {
        List<EquipmentDetailResp> respList=new ArrayList<>();
        QueryDeviceInfoBaseStationReq xunFeiApiRequest=new QueryDeviceInfoBaseStationReq();
        xunFeiApiRequest.setKeyId(request.getId());
        if(StringUtils.isEmpty(request.getType())){
            for(int i=1;i<=8;i++){
                xunFeiApiRequest.setType(String.valueOf(i));
                ApiListResponse<DeviceInfoBaseStationResp> response=xunFeiCommonApiService.getDeviceInfoBaseStation(xunFeiApiRequest,getPartyId().toString(),null,getUserId().toString());
                if(null != response.getMsg() & response.getMsg().equals("success")){
                    List<EquipmentDetailResp> resps= BeanUtil.copyList(response.getData(),EquipmentDetailResp.class);
                    respList.addAll(resps);
                }
            }
            return MultiResponse.ofWithoutTotal(respList);
        }else{
            xunFeiApiRequest.setType(request.getType());
            ApiListResponse<DeviceInfoBaseStationResp> response=xunFeiCommonApiService.getDeviceInfoBaseStation(xunFeiApiRequest,getPartyId().toString(),null,getUserId().toString());
            if(null != response.getMsg() & response.getMsg().equals("success")){
                respList= BeanUtil.copyList(response.getData(),EquipmentDetailResp.class);
                return MultiResponse.ofWithoutTotal(respList);
            }
        }
        return MultiResponse.ofWithoutTotal(respList);
    }
}
